<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="用户标识" prop="userId">
        <el-input v-model="dataForm.userId" placeholder="用户标识"></el-input>
      </el-form-item>
      <el-form-item label="商品标识" prop="goodId">
        <el-input v-model="dataForm.goodId" placeholder="商品标识"></el-input>
      </el-form-item>
      <el-form-item label="临时登陆的购物车，用户登陆后把购物车内容添加到该用户购物车下" prop="sessionId">
        <el-input v-model="dataForm.sessionId" placeholder="临时登陆的购物车，用户登陆后把购物车内容添加到该用户购物车下"></el-input>
      </el-form-item>
      <el-form-item label="商品的货号GOODS_SN" prop="goodsSn">
        <el-input v-model="dataForm.goodsSn" placeholder="商品的货号GOODS_SN"></el-input>
      </el-form-item>
      <el-form-item label="商品名称GOODS_NAME" prop="goodsName">
        <el-input v-model="dataForm.goodsName" placeholder="商品名称GOODS_NAME"></el-input>
      </el-form-item>
      <el-form-item label="市场价格,取自表市场价" prop="marketPrice">
        <el-input v-model="dataForm.marketPrice" placeholder="市场价格,取自表市场价"></el-input>
      </el-form-item>
      <el-form-item label="商品的本店价SHOP_PRICE" prop="goodsPrice">
        <el-input v-model="dataForm.goodsPrice" placeholder="商品的本店价SHOP_PRICE"></el-input>
      </el-form-item>
      <el-form-item label="商品的扩展属性, EXTENSION_CODE" prop="goodsAttr">
        <el-input v-model="dataForm.goodsAttr" placeholder="商品的扩展属性, EXTENSION_CODE"></el-input>
      </el-form-item>
      <el-form-item label="取自ECS_GOODS的IS_REAL" prop="isReal">
        <el-input v-model="dataForm.isReal" placeholder="取自ECS_GOODS的IS_REAL"></el-input>
      </el-form-item>
      <el-form-item label="商品的扩展属性,EXTENSION_CODE" prop="extensionCode">
        <el-input v-model="dataForm.extensionCode" placeholder="商品的扩展属性,EXTENSION_CODE"></el-input>
      </el-form-item>
      <el-form-item label="该商品的父商品ID,没有该值为0,有的话那该商品就是该ID的配件" prop="parentId">
        <el-input v-model="dataForm.parentId" placeholder="该商品的父商品ID,没有该值为0,有的话那该商品就是该ID的配件"></el-input>
      </el-form-item>
      <el-form-item label="购物车商品类型;0普通;1团够;2拍卖;3夺宝奇兵" prop="recType">
        <el-input v-model="dataForm.recType" placeholder="购物车商品类型;0普通;1团够;2拍卖;3夺宝奇兵"></el-input>
      </el-form-item>
      <el-form-item label="是否赠品,0否;其他, 是参加优惠活动的ID,取值于FAVOURABLE_ACTIVITY的ACT_ID" prop="isGift">
        <el-input v-model="dataForm.isGift" placeholder="是否赠品,0否;其他, 是参加优惠活动的ID,取值于FAVOURABLE_ACTIVITY的ACT_ID"></el-input>
      </el-form-item>
      <el-form-item label="是否邮寄" prop="isShipping">
        <el-input v-model="dataForm.isShipping" placeholder="是否邮寄"></el-input>
      </el-form-item>
      <el-form-item label="能否处理" prop="canHandsel">
        <el-input v-model="dataForm.canHandsel" placeholder="能否处理"></el-input>
      </el-form-item>
      <el-form-item label="该商品的属性的ID,取自GOODS_ATTR的GOODS_ATTR_ID,如果有多个,只记录了最后一个,可能是BUG" prop="goodsAttrId">
        <el-input v-model="dataForm.goodsAttrId" placeholder="该商品的属性的ID,取自GOODS_ATTR的GOODS_ATTR_ID,如果有多个,只记录了最后一个,可能是BUG"></el-input>
      </el-form-item>
      <el-form-item label="该商品的购买数量255,实际库存不减少" prop="goodAmount">
        <el-input v-model="dataForm.goodAmount" placeholder="该商品的购买数量255,实际库存不减少"></el-input>
      </el-form-item>
      <el-form-item label="操作员" prop="operator">
        <el-input v-model="dataForm.operator" placeholder="操作员"></el-input>
      </el-form-item>
      <!--<el-form-item label="创建即上传时间" prop="createUtc">
        <el-input v-model="dataForm.createUtc" placeholder="创建即上传时间"></el-input>
      </el-form-item>
      <el-form-item label="修改即更新时间" prop="modifyUtc">
        <el-input v-model="dataForm.modifyUtc" placeholder="修改即更新时间"></el-input>
      </el-form-item>
      <el-form-item label="是否删除" prop="isDeleted">
        <el-input v-model="dataForm.isDeleted" placeholder="是否删除"></el-input>
      </el-form-item>-->

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import isInteger from 'lodash/isInteger'
  import moment from 'moment'
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          userId: '',
          goodId: '',
          sessionId: '',
          goodsSn: '',
          goodsName: '',
          marketPrice: '',
          goodsPrice: '',
          goodsAttr: '',
          isReal: '',
          extensionCode: '',
          parentId: '',
          recType: '',
          isGift: '',
          isShipping: '',
          canHandsel: '',
          goodsAttrId: '',
          goodAmount: '',
          operator: '',
          createUtc: moment(new Date()).format('YYYY-MM-DD HH:mm:ss'),
          modifyUtc: moment(new Date()).format('YYYY-MM-DD HH:mm:ss'),
          isDeleted: '',
          version: ''
        },
        dataRule: {
          userId: [
            { required: true, message: '用户标识不能为空', trigger: 'blur' }
          ],
          goodId: [
            { required: true, message: '商品标识不能为空', trigger: 'blur' }
          ],
          sessionId: [
            { required: true, message: '临时登陆的购物车，用户登陆后把购物车内容添加到该用户购物车下不能为空', trigger: 'blur' }
          ],
          goodsSn: [
            { required: true, message: '商品的货号GOODS_SN不能为空', trigger: 'blur' }
          ],
          goodsName: [
            { required: true, message: '商品名称GOODS_NAME不能为空', trigger: 'blur' }
          ],
          marketPrice: [
            { required: true, message: '市场价格,取自表市场价不能为空', trigger: 'blur' }
          ],
          goodsPrice: [
            { required: true, message: '商品的本店价SHOP_PRICE不能为空', trigger: 'blur' }
          ],
          goodsAttr: [
            { required: true, message: '商品的扩展属性, EXTENSION_CODE不能为空', trigger: 'blur' }
          ],
          isReal: [
            { required: true, message: '取自ECS_GOODS的IS_REAL不能为空', trigger: 'blur' }
          ],
          extensionCode: [
            { required: true, message: '商品的扩展属性,EXTENSION_CODE不能为空', trigger: 'blur' }
          ],
          parentId: [
            { required: true, message: '该商品的父商品ID,没有该值为0,有的话那该商品就是该ID的配件不能为空', trigger: 'blur' }
          ],
          recType: [
            { required: true, message: '购物车商品类型;0普通;1团够;2拍卖;3夺宝奇兵不能为空', trigger: 'blur' }
          ],
          isGift: [
            { required: true, message: '是否赠品,0否;其他, 是参加优惠活动的ID,取值于FAVOURABLE_ACTIVITY的ACT_ID不能为空', trigger: 'blur' }
          ],
          isShipping: [
            { required: true, message: '是否邮寄不能为空', trigger: 'blur' }
          ],
          canHandsel: [
            { required: true, message: '能否处理不能为空', trigger: 'blur' }
          ],
          goodsAttrId: [
            { required: true, message: '该商品的属性的ID,取自GOODS_ATTR的GOODS_ATTR_ID,如果有多个,只记录了最后一个,可能是BUG不能为空', trigger: 'blur' }
          ],
          goodAmount: [
            { required: true, message: '该商品的购买数量255,实际库存不减少不能为空', trigger: 'blur' }
          ],
          operator: [
            { required: true, message: '操作员不能为空', trigger: 'blur' }
          ],
          createUtc: [
            { required: true, message: '创建即上传时间不能为空', trigger: 'blur' }
          ],
          modifyUtc: [
            { required: true, message: '修改即更新时间不能为空', trigger: 'blur' }
          ],
          isDeleted: [
            { required: true, message: '是否删除不能为空', trigger: 'blur' }
          ],
          version: [
            { required: true, message: '版本号不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl('/generator/upkcart/info' + (isInteger(this.dataForm.id) ? `/${this.dataForm.id}` : '')),
              method: 'get'
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.upkCart.userId
                this.dataForm.goodId = data.upkCart.goodId
                this.dataForm.sessionId = data.upkCart.sessionId
                this.dataForm.goodsSn = data.upkCart.goodsSn
                this.dataForm.goodsName = data.upkCart.goodsName
                this.dataForm.marketPrice = data.upkCart.marketPrice
                this.dataForm.goodsPrice = data.upkCart.goodsPrice
                this.dataForm.goodsAttr = data.upkCart.goodsAttr
                this.dataForm.isReal = data.upkCart.isReal
                this.dataForm.extensionCode = data.upkCart.extensionCode
                this.dataForm.parentId = data.upkCart.parentId
                this.dataForm.recType = data.upkCart.recType
                this.dataForm.isGift = data.upkCart.isGift
                this.dataForm.isShipping = data.upkCart.isShipping
                this.dataForm.canHandsel = data.upkCart.canHandsel
                this.dataForm.goodsAttrId = data.upkCart.goodsAttrId
                this.dataForm.goodAmount = data.upkCart.goodAmount
                this.dataForm.operator = data.upkCart.operator
                this.dataForm.createUtc = data.upkCart.createUtc
                this.dataForm.modifyUtc = data.upkCart.modifyUtc
                this.dataForm.isDeleted = data.upkCart.isDeleted
                this.dataForm.version = data.upkCart.version
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            var params = {
              'id': this.dataForm.id || undefined,
              'userId': this.dataForm.userId,
              'goodId': this.dataForm.goodId,
              'sessionId': this.dataForm.sessionId,
              'goodsSn': this.dataForm.goodsSn,
              'goodsName': this.dataForm.goodsName,
              'marketPrice': this.dataForm.marketPrice,
              'goodsPrice': this.dataForm.goodsPrice,
              'goodsAttr': this.dataForm.goodsAttr,
              'isReal': this.dataForm.isReal,
              'extensionCode': this.dataForm.extensionCode,
              'parentId': this.dataForm.parentId,
              'recType': this.dataForm.recType,
              'isGift': this.dataForm.isGift,
              'isShipping': this.dataForm.isShipping,
              'canHandsel': this.dataForm.canHandsel,
              'goodsAttrId': this.dataForm.goodsAttrId,
              'goodAmount': this.dataForm.goodAmount,
              'operator': this.dataForm.operator,
              'createUtc': this.dataForm.createUtc,
              'modifyUtc': this.dataForm.modifyUtc,
              'isDeleted': this.dataForm.isDeleted,
              'version': this.dataForm.version
            }
            var tick = !this.dataForm.id ? this.$http({
              url: this.$http.adornUrl('/generator/upkcart/save'),
              method: 'post',
              data: this.$http.adornData(params)
            }) : this.$http({
              url: this.$http.adornUrl('/generator/upkcart/update'),
              method: 'post',
              data: this.$http.adornData(params)
            })
            tick.then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
